import Vue from 'vue'
import Vuex from 'vuex'
import { loginApi } from '@/api/login'
import { Message } from 'element-ui'
import router from '@/router'
Vue.use(Vuex)

const state = {
  token: ''
}
const mutations = {
  setToekn (state, token) {
    state.token = token
  },
  delToekn (state, token) {
    state.token = ''
  }
}
const actions = {
  async loginAction (context, playload) {
    const res = await loginApi(playload)
    console.log(res)
    if (res.success) {
      Message.success('登录成功')
      router.push('/home')
    } else {
      Message.error('登录失败，请确认用户名或密码')
    }
    context.commit('setToekn', res.token)
  }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
